<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    .wrap {
      width: 600px;
      margin: 20px auto;
    }

    input {
      width: 500px;

    }

    textarea {
      width: 500px;
      height: 200px;
    }

    .list {
      width: 600px;
      margin: 20px auto;
    }

    .list_one {
      border: 1px solid black;
      margin-top: 15px;
    }

    .list_one>div:nth-child(1) {
      display: flex;
      line-height: 60px;
      padding: 0 50px;
      box-sizing: border-box;
      justify-content: space-between;
    }

    .list_one>div:nth-child(2) {
      line-height: 20px;
      padding-left: 40px;
      box-sizing: border-box;
    }

    .list_one>div:nth-child(3) {
      line-height: 20px;
      padding-right: 40px;
      box-sizing: border-box;
      display: flex;
      justify-content: flex-end;
    }

    .list_one>div:nth-child(3)>a {
      margin-right: 30px;
    }
  </style>
</head>

<body>
  <div class="wrap">
    <input type="text" id="title" placeholder="留言标题">
    <br>
    <textarea id="mytxt"></textarea>
    <br>
    <button id="send">留言</button>
  </div>
  <div class="list">
  </div>
  <script src="jquery-3.4.1.min.js"></script>
  <script>
    // 判断存储是否有数据
    var msglist = []
    if (localStorage.msglist) {
      msglist = JSON.parse(localStorage.msglist)
    } else {
      localStorage.msglist = JSON.stringify([])
    }
    //点击发送留言
    $("#send").click(function () {
      var obj = {
        id: +new Date(),
        title: $("#title").val(),
        msg: $("#mytxt").val(),
        date: new Date().toLocaleString(),
        zan: 0,
        low: 0
      }
      msglist.unshift(obj)
      localStorage.msglist = JSON.stringify(msglist)
      loadList()//添加完成 从新渲染列表
      // 清空输入框
      $("#title").val("")
      $("#mytxt").val("")
    })
    // 数据渲染
    function loadList() {
      var str = ''
      $.each(msglist, function (index, ele) {
        str += `
        <div class="list_one">
          <div><span>${ele.title}</span><span>${ele.date}</span></div>
          <div>${ele.msg}</div>
          <div><a href="#" onclick="zanAndCai(${ele.id},'cai')">鄙视(${ele.low})</a><a href="#" onclick="zanAndCai(${ele.id},'zan')">赞(${ele.zan})</a><a href="#" onclick="del(${index})">删除</a></div>
        </div>
        `
      })
      $(".list").html(str)
    }
    loadList()
    //删除
    function del(index) {
      msglist.splice(index, 1)
      localStorage.msglist = JSON.stringify(msglist);
      loadList();
    }
    // 点赞和鄙视
    function zanAndCai(id, status) {
      console.log(id, status)
      $.each(msglist, function (index, ele) {
        if (ele.id == id) {
          if (status == "zan") {
            ele.zan++
          } else {
            ele.low++
          }
        }
      })
      localStorage.msglist = JSON.stringify(msglist)
      loadList();
    }
  </script>
</body>

</html>